from flask import Flask, render_template, request, send_from_directory

from source.to_word_upload import to_word_upload_file, extract_ppt_titles
from source.to_ppt_upload import upload_files

app = Flask(__name__)
file_path_root = "H:\\"


# file_path_root = "/home/will"


@app.route("/", methods=["GET", "POST"])
def home():
    return render_template("index.html")


# ppt2word
# all function
# ------------------------------------------------------------
@app.route("/ppt2word", methods=["GET", "POST"])
def ppt2word():
    return render_template("ppt2word.html")


@app.route("/ppt2word/upload", methods=["GET", "POST"])
def to_word_upload():
    if request.method == "POST":
        url = to_word_upload_file()
        return render_template('ppt2word.html', download_url=url)

    return render_template("ppt2word.html")


@app.route('/ppt2word/download/<filename>', methods=['GET'])
def to_word_download(filename):
    directory = file_path_root
    return send_from_directory(directory, filename, as_attachment=True)


# ------------------------------------------------------------
# word2ppt
# all function
# ------------------------------------------------------------
@app.route("/word2ppt", methods=["GET", "POST"])
def word2ppt():
    return render_template("word2ppt.html")


@app.route('/word2ppt/upload', methods=['GET', 'POST'])
def to_ppt_upload():
    if request.method == "POST":
        filename = upload_files()
        return render_template('word2ppt.html', download_url=filename)

    return render_template("word2ppt.html")

@app.route('/word2ppt/download/<filename>', methods=['GET'])
def to_ppt_download(filename):
    directory = file_path_root
    return send_from_directory(directory, filename, as_attachment=True)

# ------------------------------------------------------------

if __name__ == "__main__":
    # app.run(port=85)
    app.run()
